package com.p2l.letterblocks;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class DialogUpdate extends Dialog {
	private ActivityLogin activity;
	private Button yes, later;

	public DialogUpdate(Context context) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		setContentView(R.layout.update);
		
		activity = (ActivityLogin) context;
		
		yes = (Button) findViewById(R.id.btnUpdateYes);
		yes.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, ActivityMain.class);
				intent.putExtra("userId", activity.userId);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				activity.startActivity(intent);
				activity.finish();

				Uri uri = Uri.parse("market://details?id=" + activity.getPackageName());
				Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
				activity.startActivity(goToMarket);
				dismiss();
			}
		});
		
		later = (Button) findViewById(R.id.btnUpdateLater);
		later.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, ActivityMain.class);
				intent.putExtra("userId", activity.userId);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				activity.startActivity(intent);
				activity.finish();
				dismiss();
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH) {
			later.performClick();
			return true;
		}
		return false;
	}
}